package cn.ustb.controller;

import cn.ustb.entity.LevelExam;
import cn.ustb.entity.LevelExamList;
import cn.ustb.entity.User;
import cn.ustb.service.LevelExamListService;
import cn.ustb.service.LevelExamService;
import cn.ustb.serviceImpl.LevelExamListServiceImpl;
import cn.ustb.serviceImpl.LevelExamServiceImpl;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * @author yuyan
 */
@WebServlet("*.apply")
public class LevelExamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	LevelExamService levelExamService = new LevelExamServiceImpl();
	LevelExamListService levelExamListService = new LevelExamListServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		//1.获取ServletPath
		String servletPath = request.getServletPath();
		//2.去除 / 和 .apply
		String methodName = servletPath.substring(1, servletPath.length() - 6);

		try {
			//3.利用反射获取methodName对应的方法
			Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
			//4.利用反射调用对应的方法
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**查询选修课程信息**/
	@SuppressWarnings("unused")
	private void query(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String yearTerm = request.getParameter("yearTerm");

		Map<LevelExam, String> map = new HashMap<>();
		List<LevelExam> levelExams = levelExamService.getAllWithYearTerm(yearTerm);
		//查询该学生已报名的课程
		for(LevelExam levelExam: levelExams){
			LevelExamList levelExamList = new LevelExamList(user.getUsername(), levelExam.getCourseId());
			String pay="0";
			if(levelExamListService.isPay(levelExamList)!=0){
				pay="1";
			}
			if(levelExamListService.get(levelExamList) != null){
				map.put(levelExam, "是"+pay);
			}else{
				map.put(levelExam, "否"+pay);
			}
		}
		session.setAttribute("mapCet", map);

		response.sendRedirect(request.getContextPath() + "/levelexamapply.jsp");
	}

	/**学生报名选修课程**/
	@SuppressWarnings("unused")
	private void apply(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String courseId = request.getParameter("courseId");


		LevelExamList levelExamList = new LevelExamList(user.getUsername(), courseId);
		//判断该课程是否已报名，防止重报名
		if(levelExamListService.get(levelExamList) != null){
			session.setAttribute("message", "该课程已报名，请勿重复报名!");

			response.sendRedirect(request.getContextPath() + "/levelexamapply.jsp");

			return;
		}
		levelExamListService.insert(levelExamList);

		session.setAttribute("message", "报名成功!");

		query(request, response);


		response.sendRedirect(request.getContextPath() + "/levelexamapply.jsp");
	}
}
